/**
 * Copyright (C) 2021 - 2031 O-Cubes Co., Ltd.
 */

/****************************************************************
 *  @file    dw_timer.h
 *  @brief   Designware timer driver header
 *  @version v1.0
 *  @date    03. Apr. 2023
 ****************************************************************/

#ifndef __DW_TIMER_H__
#define __DW_TIMER_H__

#include <stdint.h>
#include "irq.h"
#include "dev_timer.h"

#ifdef __cplusplus
extern "C" {
#endif

#define DW_TIMER_CH_MAX_COUNT               8
#define DW_TIMER_INVALID_INTNO              (DEV_INTNO_INVALID)

/**
 * @name	Structure definitions for DesignWare TIMER Register
 * @brief	Contains definitions of DesignWare TIMER register structure
 * @{
 */
typedef volatile struct dw_timer_ch_reg {
	uint32_t LOAD_COUNT;            /*!< Value to be loaded into Timer */
	uint32_t CURRENT_VALUE;         /*!< Current Value of Timer */
	uint32_t CONTROL_REG;           /*!< Control Register for Timer */
	uint32_t EOI;                   /*!< Clears the interrupt from Timer */
	uint32_t INT_STATUS;            /*!< Contains the interrupt status for Timer */
} CH_CTRL;

typedef volatile struct dw_timer_reg {
	CH_CTRL CTRL[DW_TIMER_CH_MAX_COUNT];                /*!< (0x00 ~ 0x9C) */
	uint32_t TIMERS_INT_STATUS;                         /*!< (0xA0) Contains the interrupt status of all timers in the component.*/
	uint32_t TIMERS_EOI;                                /*!< (0xA4) Read return all zeroes (0) and clears all active interrupts.*/
	uint32_t TIMERS_RAWINT_STATUS;                      /*!< (0xA8) Contains the unmasked interrupt status of all timers in the component.*/
	uint32_t TIMERS_COMP_VER;                           /*!< (0xAC) Current revision number of the DW_apb_timers component.*/
	uint32_t LOAD_COUNT2[DW_TIMER_CH_MAX_COUNT];        /*!< (0xB0 ~ 0xCC) Value to be loaded into Timer when toggle output changes from 0 to 1*/
} DW_TIMER_REG, *DW_TIMER_REG_PTR;
/** @} */

/**
 * @brief	DesignWare TIMER control structure definition
 * @details	implement of timer_info::timer_ctrl
 */
typedef struct dw_timer_ctrl {
	uint32_t id;                            /*!< timer id */
	DW_TIMER_REG_PTR regs;                  /*!< timer port register */
	uint32_t intno;                         /*!< timer interrupt vector number */
	uint32_t clock;                         /*!< timer clock */
	irq_handler int_handler;                /*!< timer interrupt handler pointer */
	DEV_TIMER_HANDLER  int_ch_handler;      /*!< interrupt handler pointer */
	DEV_TIMER_MODE     mode;                /*!< timer group  work mode array pointer*/
} DW_TIMER_CTRL, *DW_TIMER_CTRL_PTR;

/**
 * @name	DesignWare TIMER Function Declaration
 * @brief	contains declarations of designware timer functions.
 * @details	This are only used in timer object implementation source file
 * @{
 */
int32_t dw_timer_open(DEV_TIMER *timer_dev);
int32_t dw_timer_close(DEV_TIMER *timer_dev);
int32_t dw_timer_read(DEV_TIMER *timer_dev, uint32_t *mode, uint32_t *freq, uint32_t *dc);
int32_t dw_timer_write(DEV_TIMER *timer_dev, uint32_t mode, uint32_t freq, uint32_t dc);
int32_t dw_timer_control(DEV_TIMER *timer_dev, uint32_t cmd, void *param);
int32_t dw_timer_isr_handler(DEV_TIMER *timer_dev, void *ptr);

#ifdef __cplusplus
}
#endif

#endif /* __DW_TIMER_H__ */
/** @} */
